<?php
class Positionsmenu_model extends CRM_Model {
	
	public function __construct(){
		parent::__construct();
	}
	
	public function GetDS(){
		$this->db->select('_positions_menus.rid as rid, _positions_menus._positions_rid as _positions_rid, 
							_positions_menus._menu_items_rid as _menu_items_rid, _positions_menus.parent,
							_positions_menus.item_rights as item_rights,
							_positions_menus.item_area as item_area,
							_positions.name as position_name,
							_positions_menus.constant_search_rule as constant_search_rule,
							_positions_menus.default_sort_rule as default_sort_rule,
							_menu_items.item_name as menu_name,
							DATE_FORMAT(_positions_menus.modifyDT, \'%d.%m.%Y %H:%i\') as modifyDT, 
							_positions_menus.descr as descr, _positions_menus.archive as archive');
		$this->db->from('_positions_menus');
		$this->db->join('_positions', '_positions_menus._positions_rid = _positions.rid');
		$this->db->join('_menu_items', '_positions_menus._menu_items_rid = _menu_items.rid');
		if($searchRule = $this->ciObject->GetSessionData('searchrule')){
			$this->db->like($searchRule);
		}
		if($sort = $this->ciObject->GetSessionData('sort')){
			$this->db->orderby($sort['c'], $sort['r']);
		}
		$query = $this->DBGet('_users');
		if($query->num_rows()>0) return $this->ciObject->GetPagingDS($query->result());
		return array();
	}
	
	public function GetEditDS(){
		$this->db->select('_positions_menus.rid as rid, _positions_menus._positions_rid as _positions_rid, 
							_positions_menus._menu_items_rid as _menu_items_rid, _positions_menus.parent,
							_positions_menus.item_rights as item_rights,
							_positions_menus.item_area as item_area,
							_positions_menus.constant_search_rule as constant_search_rule,
							_positions_menus.default_sort_rule as default_sort_rule,
							_positions_menus.modifyDT as modifyDT, 
							_positions_menus.descr as descr, _positions_menus.archive as archive');
		$this->db->from('_positions_menus');
		$this->db->where(array('rid'=>$this->ciObject->GetSessionData('activerecord')));
		$query = $this->DBGet('_positions_menus');
		if($query->num_rows()>0) return $query->row();
		return False;
	}
	
	public function GetPositionsList(){
		$this->db->select('_positions.rid, _positions.name as name');
		$this->db->from('_positions');
		$this->db->orderby('name');
		$query = $this->DBGet('_positions');
		if($query->num_rows()>0) return $query->result();
		return False;
	}
	
	public function GetMenuitemsList(){
		$this->db->select('_menu_items.rid, _menu_items.item_name as item_name');
		$this->db->from('_menu_items');
		$this->db->orderby('item_name');
		$query = $this->DBGet('_menu_items');
		if($query->num_rows()>0) return $query->result();
		return False;
	}
	
	public function CreateRecord(){
		$insArray = array('_positions_rid'=>$_POST['_positions_rid'],
							'_menu_items_rid'=>$_POST['_menu_items_rid'],
							'parent'=>$_POST['parent'],
							'item_rights'=>$_POST['item_rights'],
							'item_area'=>$_POST['item_area'],
							'constant_search_rule'=>$_POST['constant_search_rule'],
							'default_sort_rule'=>$_POST['default_sort_rule'],
							'descr'=>$_POST['descr'],
							'archive'=>$_POST['archive'],
							'modifier_users_rid'=>$this->userRid,
							'modifyDT'=>$this->nowTime);
		$this->db->trans_begin();
		$this->db->insert('_positions_menus', $insArray);
		$insRid = $this->db->insert_id();
		if ($this->db->trans_status() === FALSE){
    		$this->db->trans_rollback();
    		return False;
		}else{
    		$this->db->trans_commit();
    		return $insRid;
		}		
	}
	
	public function UpdateRecord(){
		$updateArray = array('_positions_rid'=>$_POST['_positions_rid'],
							'_menu_items_rid'=>$_POST['_menu_items_rid'],
							'parent'=>$_POST['parent'],
							'item_rights'=>$_POST['item_rights'],
							'item_area'=>$_POST['item_area'],
							'constant_search_rule'=>$_POST['constant_search_rule'],
							'default_sort_rule'=>$_POST['default_sort_rule'],
							'descr'=>$_POST['descr'],
							'archive'=>$_POST['archive'],
							'modifier_users_rid'=>$this->userRid,
							'modifyDT'=>$this->nowTime);
		$this->db->trans_begin();
		$this->db->update('_positions_menus', $updateArray, array('rid'=>$this->ciObject->GetSessionData('activerecord')));
		if ($this->db->trans_status() === FALSE){
    		$this->db->trans_rollback();
    		return False;
		}else{
    		$this->db->trans_commit();
    		return True;
		}		
	}
	
	public function DeleteRecord(){
		$this->db->trans_begin();
		$this->db->delete('_positions_menus', array('rid'=>$this->ciObject->GetSessionData('activerecord')));
		if ($this->db->trans_status() === FALSE){
    		$this->db->trans_rollback();
    		return False;
		}else{
    		$this->db->trans_commit();
    		return True;
		}		
	}
}
?>